package com.sykj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sykj.dto.ProductInfoDto;
import com.sykj.entity.ProductInfo;
import com.sykj.entity.Result;
import com.sykj.service.IProductInfoService;
import com.sykj.util.ResultUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sykj
 * @since 2019-06-28
 */
@RestController
@RequestMapping("/productInfo")
public class ProductInfoController {

    @Autowired
    private IProductInfoService infoService;

    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * @Author vains
     * @Description //TODO 查询商品和类别信息
     * @Date 2019/6/28 17:15
     * @Param 
     * @return com.sykj.entity.Result
     **/
    @GetMapping("/queryProduct")
    public Result queryProduct(){
        Map<String, Object> objectMap = infoService.queryProduct();
        return ResultUtil.success("查询成功", objectMap);
    }

    /**
     * 根据类型获取菜品
     * @Author vains
     * @date 2019/6/29 13:25
     * @param type 菜品类型
     * @return com.sykj.entity.Result
     **/
    @GetMapping("/queryProductByType/{type}")
    public Result queryProductByType(@PathVariable("type") String type){
        QueryWrapper<ProductInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("product_status", 0);
        wrapper.eq("category_type", type);
        List<ProductInfoDto> list = new ArrayList<>();
        List<ProductInfo> infos = infoService.list(wrapper);
        infos.forEach(info -> {
            ProductInfoDto infoDto = new ProductInfoDto();
            BeanUtils.copyProperties(info, infoDto);
            infoDto.setProductQuantity(0);
            list.add(infoDto);
        });
        return ResultUtil.success("查询成功", list);
    }

    @GetMapping("/queryAll")
    public Result queryAll(){
        Map<String, Object> map = infoService.queryAll();
        return ResultUtil.success("查询成功",map);
    }

    @GetMapping("/testUpload")
    public Result testUpload() throws FileNotFoundException {
        File file = new File("E:/Javatomcat/apache-tomcat-8.0.48/headImage/1699680253.png");
        // 上传并且生成缩略图
        StorePath storePath = this.storageClient.uploadFile(
                new FileInputStream(file), file.length(), "png", null);
        // 带分组的路径
        System.out.println(storePath.getFullPath());
        // 不带分组的路径
        System.out.println(storePath.getPath());
        return ResultUtil.success("成功");
    }

}
